﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class btnPlay : MonoBehaviour {

	// Use this create object list need use
	public GameObject[] objList;

	// Use this check user click play button
	public static bool isClicked = false;
	
	// Use this check notif menu
	public static bool isSuccess = false;

	// Use this check choose result
	public static bool isResultNotif = false;

	// Use this count number question
	int maxQuest;

	// Use this for initialization
	void Start () {
		UFTButton.onButtonClick += onButtonClick;
		UFTButton.onButtonHover += onButtonHover;

		// Use this create play button animation
		iTween.RotateBy(objList[14],iTween.Hash("x", .15,"time",1, "easeType"
		                                       , "easeInOutQuad", "loopType", "pingPong", "delay", .5));	

		// Use this create next button animation
		iTween.RotateBy(objList[27],iTween.Hash("x", .15,"time",1, "easeType"
	                                        , "easeInOutQuad", "loopType", "pingPong", "delay", .2));

		// Use this create reload button animation
		iTween.RotateBy(objList[26],iTween.Hash("x", .15,"time",1, "easeType"
		                                        , "easeInOutQuad", "loopType", "pingPong"));
	}
	void onButtonClick (UFTButton button){

		// Use this when play button is clicked
		if (button.name.Equals ("btnPlay")) {
			iTween.MoveTo(objList[14],iTween.Hash("z", .1));

			// Use this move money object
			iTween.MoveTo(objList[9],iTween.Hash("x", 6.4));
			iTween.MoveTo(objList[8],iTween.Hash("x", 5.2));
			iTween.MoveTo(objList[7],iTween.Hash("x", 4));
			iTween.MoveTo(objList[6],iTween.Hash("x", 2.6));
			iTween.MoveTo(objList[5],iTween.Hash("x", 1.2));
			iTween.MoveTo(objList[4],iTween.Hash("x", -0.2));
			iTween.MoveTo(objList[3],iTween.Hash("x", -1.2));
			iTween.MoveTo(objList[2],iTween.Hash("x", -2.2));
			iTween.MoveTo(objList[1],iTween.Hash("x", -3.6));
			iTween.MoveTo(objList[0],iTween.Hash("x", -4.8));

			// Use this create animation for award object
			iTween.RotateBy(objList[9],iTween.Hash("x", .15,"time",1, "easeType"
			                                        , "easeInOutQuad", "loopType", "pingPong"));
			// Use this move character and idea object
			iTween.MoveTo(objList[17],iTween.Hash("x", -6.2));
			iTween.MoveTo(objList[18],iTween.Hash("x", -6.2));
			iTween.MoveTo(objList[16],iTween.Hash("x", -6.2));

			// Use this move answer button object
			iTween.MoveTo(objList[10],iTween.Hash("x", 2));
			iTween.MoveTo(objList[24],iTween.Hash("x", -2));

			// Use this move go label object
			iTween.MoveTo(objList[15],iTween.Hash("position",transform.position += Vector3.right*22,"easetype",iTween.EaseType.easeInOutSine,"time", 1));

			// Use this move question board object
			iTween.MoveTo(objList[11],iTween.Hash("y", 1.3,
			                                      "speed", 5, 
			                                      "oncomplete", "completed",
			                                      "oncompletetarget", gameObject));

			// Use this move progress time bar object
			iTween.MoveTo(objList[29],iTween.Hash("z", -1));
		}

		// Use this when answer 1 button is clicked
		if (button.name.Equals ("btnAnswer 1")) {

			// Use this increase question number
			maxQuest ++;

			if(QuestionLabel.questionIndex < 10){
				if(AnswerLabel1.randomNumberList[QuestionLabel.questionIndex] == 0){
					iTween.PunchPosition(objList[12],iTween.Hash("z", 1, 
					                                             "time", 1));

				if(QuestionLabel.questionIndex < 10){

						// Use this increase point
						Program.score ++;

						// Use this move character and idea object
						iTween.MoveTo(objList[17],iTween.Hash("x", objList[Program.score - 1].transform.position.x));
						iTween.MoveTo(objList[18],iTween.Hash("x", objList[Program.score - 1].transform.position.x));
						iTween.MoveTo(objList[16],iTween.Hash("position", objList[18].transform.position));

						// Use this move IQs object
						iTween.MoveTo(objList[16],iTween.Hash("position", objList[19].transform.position,
						                                      "delay", .1));

						                                                     					                                                  
						// Use this move money object
						iTween.MoveTo(objList[Program.score - 1],iTween.Hash("position", objList[20].transform.position, 
						                                                     "z", -1,
						                                                     "oncomplete", "resultNotif",
						                                                     "oncompletetarget", gameObject));

						// Use this move money total object
						iTween.PunchPosition(objList[21],iTween.Hash("z", 1,
						                                             "delay", .3, 
							                                         "time", 1));

						// Use this move money object
						iTween.MoveTo(objList[Program.score - 1],iTween.Hash("z", 1,
						                                                     "delay", .3,
						                                                     "time", 1));

						// Use this move IQs total object
						iTween.PunchPosition(objList[25],iTween.Hash("z", 1,
						                                             "delay", .3,
						                                             "time", 1));
					}
				}else
					iTween.PunchPosition(objList[13],iTween.Hash("z", 1, 
					                                             "time", 1));	

			}
			if(QuestionLabel.questionIndex < 9){
				QuestionLabel.questionIndex ++;
			}

			// Use this stop game
			if(maxQuest > 9){
				Program.isStop = true;
			}

		}
		// Use this when answer 2 button is clicked
		if (button.name.Equals ("btnAnswer 2")) {

			// Use this increase question number
			maxQuest ++;

			if(QuestionLabel.questionIndex < 10){
				if(AnswerLabel1.randomNumberList[QuestionLabel.questionIndex] == 1){
					iTween.PunchPosition(objList[12],iTween.Hash("z", 1, 
					                                             "time", 1));
					if(QuestionLabel.questionIndex < 10){

						// Use this increase point
						Program.score ++;

						// Use this move character and idea object
						iTween.MoveTo(objList[17],iTween.Hash("x", objList[Program.score - 1].transform.position.x));
						iTween.MoveTo(objList[18],iTween.Hash("x", objList[Program.score - 1].transform.position.x));
						iTween.MoveTo(objList[16],iTween.Hash("position", objList[18].transform.position));

						// Use this move IQs object
						iTween.MoveTo(objList[16],iTween.Hash("position", objList[19].transform.position,
						                                      "delay", .1));

						// Use this move money object
						iTween.MoveTo(objList[Program.score - 1],iTween.Hash("position", objList[20].transform.position, 
						                                                     "z", -1,
						                                                     "oncomplete", "resultNotif",
						                                                     "oncompletetarget", gameObject));

						// Use this move total money object
						iTween.PunchPosition(objList[21],iTween.Hash("z", 1,
						                                             "delay", .3,
							                                          "time", 1));

						// Use this move money object
						iTween.MoveTo(objList[Program.score - 1],iTween.Hash("z", 1,
						                                                     "delay", .3,
						                                             		 "time", 1));

						// Use this move IQs total object
						iTween.PunchPosition(objList[25],iTween.Hash("z", 1,
						                                             "delay", .3,
						                                             "time", 1));

					}
				}else
					iTween.PunchPosition(objList[13],iTween.Hash("z", 1, 
					                                             "time", 1));	
			}
			if(QuestionLabel.questionIndex < 9){
				QuestionLabel.questionIndex ++;				
			}

			// Use this stop game
			if(maxQuest > 9){
				Program.isStop = true;
			}
		}

		// Use this if user choose next level button
		if (button.name.Equals ("NextButton")) {
			Application.LoadLevel("Level 2");
		}

		// Use this if user choose play again button
		if (button.name.Equals ("ReloadButton")) {
			Application.LoadLevel(Application.loadedLevel);
		}
	}

	public bool completed(){
		return isClicked = true;
	}
	public bool resultNotif(){
		return isResultNotif = true;
	}
	void onButtonHover (UFTButton button){

	}

	void Update(){
		// Use this stop game
		if (Program.isStop == true) {
			// Use this 
			isClicked = false;

			Debug.Log("time: " + Program.timeTotal + "/remain time :" + (Program.remainTime));


			// Use this move & hide object
			iTween.MoveTo(objList[24],iTween.Hash("z", .1));
			iTween.MoveTo(objList[10],iTween.Hash("z", .1));
			iTween.MoveTo(objList[22],iTween.Hash("z", .1));
			iTween.MoveTo(objList[23],iTween.Hash("z", .1));
			iTween.MoveTo(objList[11],iTween.Hash("z", .1));
			iTween.MoveTo(objList[28],iTween.Hash("z", .1));
			iTween.MoveTo(objList[29],iTween.Hash("z", .1));
			
			// Use 2 next button and reload button
			iTween.MoveTo(objList[26],iTween.Hash("z", -1));
			iTween.MoveTo(objList[27],iTween.Hash("z", -1));

			// Use this move notif board object
			iTween.MoveTo(objList[30],iTween.Hash("y", 0.5,
			                                      "speed", 10));
			if(Program.score < 8){
				iTween.MoveTo(objList[35],iTween.Hash("y", 0.5,
				                                      "speed", 10));
			}else{
				iTween.MoveTo(objList[34],iTween.Hash("y", 0.5,
				                                      "speed", 10));
			}

			// Use this check correct answer number
			switch(Program.score){
				case 8:
					iTween.MoveTo(objList[31],iTween.Hash("y", 1.7,
					                                      "speed", 10,
					                                      "delay", 0.2,
				                                     	  "oncomplete", "NotifMenuSuccess",
					                                      "oncompletetarget", gameObject));
					break;
				case 9:
					iTween.MoveTo(objList[31],iTween.Hash("y", 1.7,
					                                      "speed", 10,
					                                      "delay", 0.2));
					iTween.MoveTo(objList[32],iTween.Hash("y", 2,
					                                      "speed", 10,
					                                      "delay", 0.4,
				                                     	  "oncomplete", "NotifMenuSuccess",
					                                      "oncompletetarget", gameObject));
					break;
				case 10:
					iTween.MoveTo(objList[31],iTween.Hash("y", 1.7,
					                                      "speed", 10,
					                                      "delay", 0.2));
					iTween.MoveTo(objList[32],iTween.Hash("y", 2,
					                                      "speed", 10,
					                                      "delay", 0.4));
					iTween.MoveTo(objList[33],iTween.Hash("y", 1.7,
					                                      "speed", 10,
					                                      "delay", 0.6,
				                                     	  "oncomplete", "NotifMenuSuccess",
					                                      "oncompletetarget", gameObject));
					break;
			}

			Program.isStop = false;
		}
	}

	// Use this check notif menu success
	private bool NotifMenuSuccess(){
		return (isSuccess == true);
	}
	public static btnPlay Instance;
	void Awake()
	{

		if(Instance)
			DestroyImmediate(gameObject);
		else
		{
			DontDestroyOnLoad(gameObject);
			for(int i = 0; i < objList.Length; i++){
				DontDestroyOnLoad(objList[i]);
			}
			Instance = this;
		}
	}
}
